// pages/regist/regist.js
import {ajax} from "../../../utils/httpRequest";
import {URI_GETCOMPANYLIST, URI_GETBRANCHLIST, URI_INFO_ENTRY} from '../../../utils/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    password: '',
    password1: '',
    branchs_id: '',
    party_status: 0,
    tel: '',
    company_id: '',
    uid: '',
    companyList: [],
    organizationList: [],
    politicalList: ['党员', '群众'],
    companyindex: 0,
    organizationindex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    ajax.get(URI_GETCOMPANYLIST, {}, {}, res => {
      console.log(res.data);
      that.setData({
        companyList: res.data,
        company_id: res.data[0].id
      });
      this.getBranchlist();
    });
    this.setData({
      tel: wx.getStorageSync('tel')
    })
  },
  //通过company_id获取公司的组织
  getBranchlist() {
    ajax.post(URI_GETBRANCHLIST, {
      company_id: this.data.company_id
    }, {}, res => {
      if (res.data.length > 0) {
        this.setData({
          organizationList: res.data,
          branchs_id: res.data[0].id
        })
      }
    });
  },
  nameInput: function (e) {
    this.setData({
      username: e.detail.value
    })
  },

  passwordInput: function (e) {
    var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;
    var passWord = e.detail.value;
    if(!reg.test(passWord)){
      wx.showToast({
        title: '密码要求大于8位小于16位，字母和数字',
        icon: 'none',
        duration: 2500
      });
      this.setData({
        password: ''
      });
      return false;
    }
    this.setData({
      password: passWord
    })
  },
  passwordInput1: function (e) {
    this.setData({
      password1: e.detail.value
    })
  },


  //公司选择
  bindCompanyChange: function (e) {
    var that = this;
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      companyindex: e.detail.value,
      company_id: this.data.companyList[e.detail.value].id
    });
    this.getBranchlist();
  },


  //公司的内部组织
  bindOrganizationChange: function (e) {
    var that = this;
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      organizationindex: e.detail.value,
      branchs_id: this.data.organizationList[e.detail.value].id
    })
  },


  //政治面貌
  bindPoliticalChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      party_status: e.detail.value
    })
  },

  goToIndex: function () {
    if(this.data.password===this.data.password1){
      ajax.post(URI_INFO_ENTRY, {
        username: this.data.username,
        password: this.data.password,
        password1: this.data.password1,
        branchs_id: this.data.branchs_id,
        party_status: parseInt(this.data.party_status)+1,
        tel: this.data.tel,
        company_id: this.data.company_id,
        uid: '',
        only_u:wx.getStorageSync('only_u'),
        faceUrl:wx.getStorageSync('faceUrl'),
      }, {}, res => {
        wx.showModal({
          title:'温馨提示',
          content:'信息录入成功！返回登录',
          confirmColor: '#0d77e4',
          showCancel:false,
          success:(res)=>{
            if(res.confirm){
              wx.redirectTo({
                url: '../authorize/authorize',
              })
            }
          }
        });
      });
    }else {
      wx.showToast({
        title: '两次输入密码不一致',
        icon: 'none',
        duration: 2500
      });
    }
  }
});